package com.ruoyi.web.mnt.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.trans.core.TranslateResponse;
import com.ruoyi.web.mnt.domain.bo.MntHistoryBo;
import com.ruoyi.web.mnt.domain.vo.MntHistoryVo;
import com.ruoyi.web.mnt.service.IMntHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 设施巡维记录Controller
 *
 * @author LIUJUN
 * @date 2022-06-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mnt/demo")
public class MntHistoryController extends BaseController {

    private final IMntHistoryService iMntHistoryService;

    /**
     * 查询设施巡维记录列表
     */
    @GetMapping("/list")
    @TranslateResponse
    public List<MntHistoryVo> list(@Validated(QueryGroup.class) MntHistoryBo bo, PageQuery pageQuery) {
        return iMntHistoryService.queryPageList(bo, pageQuery).getRows();
    }

    /**
     * 查询设施巡维记录列表
     */
    @GetMapping("/tableInfo")
    public TableDataInfo<MntHistoryVo> tableInfo(@Validated(QueryGroup.class) MntHistoryBo bo, PageQuery pageQuery) {
        return iMntHistoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设施巡维记录列表
     */
    @SaCheckPermission("demo:mnt:export")
    @Log(title = "设施巡维记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated MntHistoryBo bo, HttpServletResponse response) {
        List<MntHistoryVo> list = iMntHistoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "设施巡维记录", MntHistoryVo.class, response);
    }

    /**
     * 获取设施巡维记录详细信息
     */
    @SaCheckPermission("demo:mnt:query")
    @GetMapping("/{id}")
    public R<MntHistoryVo> getInfo(
            @NotNull(message = "主键不能为空")
            @PathVariable("id") Long id) {
        return R.ok(iMntHistoryService.queryById(id));
    }

    /**
     * 新增设施巡维记录
     */
    @SaCheckPermission("demo:mnt:add")
    @Log(title = "设施巡维记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MntHistoryBo bo) {
        return toAjax(iMntHistoryService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改设施巡维记录
     */
    @SaCheckPermission("demo:mnt:edit")
    @Log(title = "设施巡维记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MntHistoryBo bo) {
        return toAjax(iMntHistoryService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除设施巡维记录
     */
    @SaCheckPermission("demo:mnt:remove")
    @Log(title = "设施巡维记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(
            @NotEmpty(message = "主键不能为空")
            @PathVariable Long[] ids) {
        return toAjax(iMntHistoryService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
